package cn.w2.demo1.aio.server;

import java.net.InetSocketAddress;
import java.nio.channels.AsynchronousChannelGroup;
import java.nio.channels.AsynchronousServerSocketChannel;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executors;

public class AioServer extends Thread {
	private AsynchronousServerSocketChannel serverSocketChannel;

	@Override
	public void run() {
		try {
			serverSocketChannel = AsynchronousServerSocketChannel
					.open(AsynchronousChannelGroup.withCachedThreadPool(Executors.newCachedThreadPool(), 10));
			serverSocketChannel.bind(new InetSocketAddress(7397));
			System.out.println("itstack-demo-netty aio server start done. {关注公众号：bugstack虫洞栈 | 欢迎关注&获取源码}");
			// 等待
			CountDownLatch latch = new CountDownLatch(1);
			serverSocketChannel.accept(this, new AioServerChannelInitializer());
			latch.await();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public AsynchronousServerSocketChannel serverSocketChannel() {
		return serverSocketChannel;
	}

	public static void main(String[] args) {
		new AioServer().start();
	}
}
